package com.hp.upload_service.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploadController {

    //Fastdfs所在服务器的地址
    public static final String DIR = "http://192.168.31.165/";

    //fast客户端
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @PostMapping("/upload")
    public ResponseEntity<String> upload(MultipartFile file) throws IOException {
        //截取文件后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传到Fastdfs
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        //返回上传文件路径给客户端
        return ResponseEntity.ok(DIR + storePath.getFullPath());
    }
}
